<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="180">
      </el-table-column>
      <el-table-column prop="configName" label="配置名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="configKey" label="配置key" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="configValue" label="配置value" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="configType" label="配置状态" width="80"  sortable>
      </el-table-column>


      <el-table-column prop="createBy" label="创建人" width="80"  sortable>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="updateBy" label="修改人" width="180"  sortable>
      </el-table-column>

      <el-table-column prop="updateTime" label="修改时间" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="remark" label="备注" width="300"  sortable>
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="query.rows" :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="saveConfigDate" :close-on-click-modal="false">
      <el-form :model="savaForm" label-width="80px" :rules="savaFormRules" ref="savaForm">
        <el-form-item label="配置名称" >
          <el-input v-model="savaForm.configName" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="key" >
          <el-input v-model="savaForm.configKey" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="key" >
          <el-input v-model="savaForm.configValue" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" >
          <el-input v-model="savaForm.configType" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" >
          <el-input v-model="savaForm.remark" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveConfigDate = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>


export default {
  data() {
    return {
      filters: {
        name: ''
      },
      users: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列
      checkAll:false,
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      saveConfigDate: false,//新增界面是否显示
      addLoading: false,
      savaFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入标识', trigger: 'blur' }
        ]
      },
      //新增界面数据
      savaForm: {
        id:null,
        configName:'',
        configKey:'',
        configValue:'',
        configType:'',
        remark:''
      },
      pageInfo:{
        rows:[],
        total:0
      },
      query:{
        rows:5,
        page:1,
        keyword:''
      },


    }
  },
methods:{

    //页面刷新
    flushDate(){
      this.getConfigs()
    },

    search(){
      this.query.page=1;
      this.getConfigs();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getConfigs();
    },
    //获取用户列表
    getConfigs() {
      this.$http.post("/sysmanage/config/list",this.query)
          .then(result=>{
            result=result.data;

            if(result.success){
              this.pageInfo=result.resultObj;
              console.log(result.resultObj)

            }
          })

    },
    //删除
    handleDel: function (index,row) {

      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        console.log(row);
        let configId=row.configId

        this.$http.delete("/sysmanage/config/"+configId)
            .then(result =>{

              result=result.data
              if(result.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '删除失败'+result.message,
                  type: 'error'
                });
              }
              // //刷新页面
              this.search();
            })
      }).catch(() => {
        this.$message({
          message: '已取消',
          type: 'info'
        });
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveConfigDate = true;
      this.savaForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.saveConfigDate = true;
      this.savaForm = {
        id:null,
        configName:'',
        configKey:'',
        configValue:'',
        configType:'',
        remark:''
      };
    },
    //新增
    addSubmit: function () {
      this.$refs.savaForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {

            this.$http.put("/sysmanage/config",this.savaForm)
                .then(result =>{
                  this.listLoading=false;
                  result=result.data
                  if(result.success){
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '保存失败'+result.message,
                      type: 'error'
                    });
                  }
                  //刷新页面
                  this.saveConfigDate=false;
                  this.search();
                })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.patch("/config",ids)
            .then(result =>{
              this.listLoading=false;

              result=result.data
              if(result.success){
                this.$message({
                  message: '批量删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '批量删除失败'+result.message,
                  type: 'error'
                });
              }
              //刷新页面
              this.search();
            })


      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getConfigs();
    // this.getAllMenuName()
  }
}

</script>

<style scoped>

</style>